package com.atguigu.spzx.manager.config;

import com.atguigu.spzx.manager.properties.UserAuthProperties;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * ClassName: WebMvcConfiguration
 * Package: com.atguigu.spzx.manager.config
 * Description:
 *
 * @Author 刘强
 * @Create 2024/6/29 15:46
 * @Version 1.0
 */

/**
 * SpringMvc的配置类
 *      配置拦截器，解决跨域问题
 *
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Resource
    private UserAuthProperties userAuthProperties;
    @Resource
    private LoginAuthInterceptor loginAuthInterceptor;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 要拦截的请求路径
                .allowedOriginPatterns("*")     // 允许的请求源(ip+端口)
                .allowedMethods("*")        // 允许的请求方式
                .allowedHeaders("*")        // 允许的请求头
                .allowCredentials(true);    // 允许携带cookie
    }

    /**
     * 注册登录认证拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(userAuthProperties.getNoAuthUrls());

    }
}
